﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.SharePointPeople;

//CID 0749 //2011.09.21 //Begin
namespace InfoJetSoft.Service.Worker
{
    class DigitalSignatureViewWorker
    {
        public static string viewSignature(Form form)
        {
            string ajaxBracket = FormUtil.getAjaxBracket(form);
            
            StringBuilder buffer = new StringBuilder();
            buffer.Append(ajaxBracket);
            string parent = "";
            if (ConfigUtil.UseAJAX())
            {
                parent = "";
            }
            else
            {
                buffer.Append("<script>");
                parent = "parent.";
            }
            buffer.Append(parent + "InfoJet_StopProgress();");
            buffer.Append(parent + "InfoJet_Postbacking = 0;");
            buffer.Append(parent + "InfoJet_OnPostbackEnd();");

            try
            {
                FormParseRequestParamWorker.parseRequestParam(form);
                FormBuildXMLDocByFieldListWorker.buildXMLDocByFieldList(form);

                string sigid = form.HttpContext.Request.Params["xdoc_param_dsig_sigid"];
                XmlElement signatureElement = DOMUtil.findNodeByJetId(form, sigid) as XmlElement;

                //CID 1021 //2011.11.23 //Begin
                SignedDataBlock signedDataBlock = null;
                foreach (SignedDataBlock block in form.Manifest.SignedDataBlockList)
                {
                    XmlElement containerElement = DOMUtil.selectANode(form.XmlDoc.DocumentElement, block.Location, form.XmlNsResolver) as XmlElement;
                    if (containerElement != null)
                    {
                        if (signatureElement.ParentNode.Equals(containerElement))
                        {
                            signedDataBlock = block;
                            break;
                        }
                    }
                }
                //CID 1021 //2011.11.23 //End

                SignedStatus status = DigitalSignatureChecker.CheckSignature(form, signatureElement, signedDataBlock.Mode);

                string id = signatureElement.GetAttribute("Id");
                XmlElement propertyElement = QuickSelectUtil.SearchChildElementByLocalName(signatureElement, "SignatureProperty");
                XmlElement Comment = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "Comment");
                XmlElement UntrustedSystemDateTime = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "UntrustedSystemDateTime");
                XmlElement OperatingSystem = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "OperatingSystem");
                XmlElement Browser = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "Browser");
                XmlElement NrOfMonitors = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "NrOfMonitors");
                XmlElement Width = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "Width");
                XmlElement Height = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "Height");
                XmlElement ColorDepth = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "ColorDepth");
                XmlElement SolutionFingerprint = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "SolutionFingerprint");
                XmlElement CurrentView = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "CurrentView");
                XmlElement ScreenDumpPNG = QuickSelectUtil.SearchChildElementByLocalName(propertyElement, "ScreenDumpPNG");

                SignedData signedData = new SignedData();
                signedData.Status = status.ToString();
                signedData.DataName = id;
                signedData.Comment = Comment.InnerText.Trim();
                DateTime dateTime = DateTime.ParseExact(UntrustedSystemDateTime.InnerText.Trim(), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture);
                signedData.DateTime = dateTime.ToString();
                signedData.OS = OperatingSystem.InnerText.Trim();
                if (Browser != null)
                {
                    signedData.Browser = Browser.InnerText.Trim();
                }
                else
                {
                    signedData.Browser = "(n/a)";
                }
                signedData.NrOfMonitors = NrOfMonitors.InnerText.Trim();
                signedData.PrimaryMonitor = Width.InnerText.Trim() + "px x " + Height.InnerText.Trim() + " - " + ColorDepth.InnerText.Trim() + "bpp";
                signedData.Fingerprint = SolutionFingerprint.InnerText.Trim();
                signedData.ViewName = CurrentView.InnerText.Trim();
                signedData.Screenshot = ScreenDumpPNG.InnerText.Trim();

                //CID 1504 //2012.12.23 //Begin
                signedData.FieldId = sigid;
                //CID 1504 //2012.12.23 //End
                //CID 1538 //2013.01.11 //Begin
                signedData.Name = signedDataBlock.Name;
                //CID 1538 //2013.01.11 //End

                XmlElement X509Certificate = QuickSelectUtil.SearchChildElementByLocalName(signatureElement, "X509Certificate");
                signedData.Cert = X509Certificate.InnerText.Trim();

                string jsData = Newtonsoft.Json.JavaScriptConvert.SerializeObject(signedData);
                buffer.Append("var InfoJet_CurrentSignedData = ");
                buffer.Append(jsData);
                buffer.Append(";");

                buffer.Append("InfoJet_ViewSignature(InfoJet_CurrentSignedData);");
            }
            catch (Exception e)
            {
                buffer.Append(parent + "alert(\"" + e.Message + "\");");
            }

            if (!ConfigUtil.UseAJAX())
            {
                buffer.Append("</script>");
            }

            buffer.Append(ajaxBracket);
            return buffer.ToString();
        }
    }

    class SignedData
    {
        public string Status
        {
            get;
            set;
        }

        public string DataName
        {
            get;
            set;
        }

        public string Comment
        {
            get;
            set;
        }

        public string DateTime
        {
            get;
            set;
        }

        public string Fingerprint
        {
            get;
            set;
        }

        public string ViewName
        {
            get;
            set;
        }

        public string OS
        {
            get;
            set;
        }

        public string Browser
        {
            get;
            set;
        }

        public string NrOfMonitors
        {
            get;
            set;
        }

        public string PrimaryMonitor
        {
            get;
            set;
        }

        public string Screenshot
        {
            get;
            set;
        }

        public string Cert
        {
            get;
            set;
        }

        //CID 1504 //2012.12.23 //Begin
        public string FieldId
        {
            get;
            set;
        }
        //CID 1504 //2012.12.23 //End

        //CID 1538 //2013.01.11 //Begin
        public string Name
        {
            get;
            set;
        }
        //CID 1538 //2013.01.11 //End
    }
}
//CID 0749 //2011.09.21 //End